/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.db.util;

import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edi.db.Session;
import com.edi.db.SessionFactory;

/**
 * sessionUtil 管理session， 管理数据库级的事务
 * 
 */
public class SessionUtil {
	private static final Log log = LogFactory.getLog(SessionUtil.class);
	private SessionFactory sessionFactory;
	private ThreadLocal localSessions = new ThreadLocal();

	public SessionUtil(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session openSession() {
		Session session = null;
		try {
			if (sessionFactory == null)
				throw new Exception("sessionFactory 为空！");
			if (localSessions.get() == null) {
				session = sessionFactory.openSession();
				if (session != null)
					localSessions.set(session);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return session;
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public Session getSession() {
		Session session = (Session) localSessions.get();
		if (session == null) {
			session = openSession();
			localSessions.set(session);
		} else if (!session.isOpen()) {
			session = openSession();
			localSessions.set(session);
		}
		return session;
	}

	/**
	 * 关闭session
	 * 
	 */
	public void closeSession() {
		Session session = (Session) localSessions.get();
		//		log.debug("当前session为： "+session);
		localSessions.set(null);
		if (session != null) {
			//			log.debug("打开状态为："+session.isOpen());
			if (session.isOpen())
				session.close();
			session = null;
		}
	}

	/**
	 * 开始事务
	 * 
	 */
	public void beginTransaction() {
		Session session = (Session) localSessions.get();
		if (session != null)
			try {
				session.beginTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * 提交事务
	 * 
	 */
	public void commitTransaction() {
		Session session = (Session) localSessions.get();
		if (session != null)
			try {
				session.commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * 回滚事务
	 * 
	 */
	public void rollbackTransaction() {
		Session session = (Session) localSessions.get();
		if (session != null)
			try {
				session.rollbackTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
